package com.taobao.wolfwolf.taobao;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.util.Calendar;

import org.apache.log4j.Logger;

import com.taobao.wolfwolf.hibernate.pojo.ReceiverAddress;
import com.taobao.wolfwolf.hibernate.pojo.ReceiverAddress1;
import com.taobao.wolfwolf.util.Util;

/**
 * <p>打印快递单<p>
 * @author qinanhg
 * 创建日期: 2012-3-12 下午12:23:54
 */
public class PrintKd implements Printable {
	private ReceiverAddress1 raddress;
	private com.taobao.wolfwolf.hibernate.pojo.Expressfieldposition fieldPosition;
	private String serialCode;
	private String expressName;
	private static Logger logger = Logger.getLogger(PrintKd.class);
	
	public PrintKd(com.taobao.wolfwolf.hibernate.pojo.Express express, ReceiverAddress1 raddress) {
		this.raddress = raddress;
		this.fieldPosition = express.getExpressfieldposition();
		this.serialCode = String.valueOf(express.getCount());
		this.expressName = express.getCodeName();
		
		printTask();
	}
	
	/**
	 * <p>设置打印属性<p>
	 */
	private void printTask() {
		// 通俗理解就是书、文档
		Book book = new Book();
		// 设置成竖打
		PageFormat pf = new PageFormat();

		pf.setOrientation(PageFormat.PORTRAIT);
		
		// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
		Paper p = new Paper();

		
		float oneMM = (float) (72 / 25.4);
		double x = fieldPosition.getHeight() + 0.7;
		
		// 纸张大小(要转换成点)
		p.setSize(fieldPosition.getWidth() * oneMM, x * oneMM); 
		
		p.setImageableArea(0, 0, fieldPosition.getWidth() * oneMM, fieldPosition.getHeight() * oneMM); 
		pf.setPaper(p);
		
		// 把 PageFormat 和 Printable 添加到书中，组成一个页面
		book.append(this, pf);
		
		// 获取打印服务对象
		PrinterJob job = PrinterJob.getPrinterJob();
		
		// 设置打印类
		job.setPageable(book);
		
		try {
			job.print();
		} catch (PrinterException e) {
			e.printStackTrace();
		}
	}

	public int print(Graphics gp, PageFormat pf, int page)
			throws PrinterException {
		Graphics2D g2 = (Graphics2D) gp;
		
		// 设置打印颜色为黑色
		g2.setPaint(Color.black); 
		
		// 当打印页号大于需要打印的总页数时，打印工作结束
		if (page >= 1) 
			return Printable.NO_SUCH_PAGE;
		
		// 转换坐标，确定打印边界
		g2.translate(pf.getImageableX(), pf.getImageableY());
		
		
		// 创建字体
		Font font = new Font("宋体", Font.PLAIN, 12);
		Font small_font = new Font("宋体", Font.PLAIN, 10);
		
		g2.setFont(font);
		
	    // 1点为1英寸的1/72,1英寸为25.4毫米,所以A4纸大致为595x842点
		// 毫米转化为点
		float oneMM = (float) (72 / 25.4); 
		
		// 打印寄件方
		g2.drawString("安娜", Util.getFieldPostionX(fieldPosition.getSenderName()) * oneMM, Util.getFieldPostionY(fieldPosition.getSenderName()) * oneMM);
		
		if(fieldPosition.getSourceCity() != null)
			g2.drawString("柳州", Util.getFieldPostionX(fieldPosition.getSourceCity()) * oneMM, Util.getFieldPostionY(fieldPosition.getSourceCity()) * oneMM);
		g2.drawString("淘宝旺旺ID: myleft1", Util.getFieldPostionX(fieldPosition.getCompanyName()) * oneMM, Util.getFieldPostionY(fieldPosition.getCompanyName()) * oneMM);
		g2.drawString(" http://snowsnow.taobao.com", Util.getFieldPostionX(fieldPosition.getSenderAddress1()) * oneMM, Util.getFieldPostionY(fieldPosition.getSenderAddress1()) * oneMM);
		g2.drawString("雪雪原创 蝴蝶结专卖店 婚纱礼服蝴蝶结定制", Util.getFieldPostionX(fieldPosition.getSenderAddress2()) * oneMM, Util.getFieldPostionY(fieldPosition.getSenderAddress2()) * oneMM);
		g2.drawString("0772-2818316", Util.getFieldPostionX(fieldPosition.getSellerPhone()) * oneMM, Util.getFieldPostionY(fieldPosition.getSellerPhone()) * oneMM);
		
		 
		 if(expressName == "STO")
			 g2.drawString("蝴蝶结   " + serialCode, (Util.getFieldPostionX(fieldPosition.getSellGoods()) - (3 + serialCode.length()) / 2) * oneMM, Util.getFieldPostionY(fieldPosition.getSellGoods()) * oneMM);
		 else
			 g2.drawString("蝴蝶结", Util.getFieldPostionX(fieldPosition.getSellGoods()) * oneMM, Util.getFieldPostionY(fieldPosition.getSellGoods()) * oneMM);
		
		if(fieldPosition.getSellerSign() != null)
			g2.drawString("安娜", Util.getFieldPostionX(fieldPosition.getSellerSign()) * oneMM, Util.getFieldPostionY(fieldPosition.getSellerSign()) * oneMM);
		if(fieldPosition.getYear() != null)
			g2.drawString(getData()[0], Util.getFieldPostionX(fieldPosition.getYear()) * oneMM, Util.getFieldPostionY(fieldPosition.getYear()) * oneMM);
		g2.drawString(getData()[1], Util.getFieldPostionX(fieldPosition.getMonth()) * oneMM, Util.getFieldPostionY(fieldPosition.getMonth()) * oneMM);
		g2.drawString(getData()[2], Util.getFieldPostionX(fieldPosition.getDay()) * oneMM, Util.getFieldPostionY(fieldPosition.getDay()) * oneMM);
		
		// 打印收件方
		g2.drawString(raddress.getReceiver_name(), Util.getFieldPostionX(fieldPosition.getReceiverName()) * oneMM, Util.getFieldPostionY(fieldPosition.getReceiverName()) * oneMM);
		if(fieldPosition.getDestinationCity() != null)
			g2.drawString(raddress.getReceiver_txt_city(), Util.getFieldPostionX(fieldPosition.getDestinationCity()) * oneMM, Util.getFieldPostionY(fieldPosition.getDestinationCity()) * oneMM);
		g2.drawString("买家ID: " + raddress.getBuyer_nick(), Util.getFieldPostionX(fieldPosition.getReceiverCompany()) * oneMM, Util.getFieldPostionY(fieldPosition.getReceiverCompany()) * oneMM);
		
		String state = raddress.getReceiver_txt_state();
		String city = raddress.getReceiver_txt_city();
		String district = raddress.getReceiver_txt_district();
		String address = filterNullAddress(state) + filterNullAddress(city) + filterNullAddress(district) + raddress.getReceiver_street();
		
		g2.setFont(small_font);
		
		float address_font_width = g2.getFontMetrics().stringWidth(address);
		float address_field_width = Util.getFieldPostionWidth(fieldPosition.getReceiverAddress1()) * oneMM;
		
		if(address_font_width - address_field_width <= 0) {
			g2.drawString(address, Util.getFieldPostionX(fieldPosition.getReceiverAddress1()) * oneMM, Util.getFieldPostionY(fieldPosition.getReceiverAddress1()) * oneMM);
		} else {
			int a = (int) (address_font_width - address_field_width) / small_font.getSize() + 3;
			String address1 = address.substring(0, address.length() - a);
			String address2 = address.substring(address.length() - a);
			g2.drawString(address1, Util.getFieldPostionX(fieldPosition.getReceiverAddress1()) * oneMM, Util.getFieldPostionY(fieldPosition.getReceiverAddress1()) * oneMM);
			g2.drawString(address2, Util.getFieldPostionX(fieldPosition.getReceiverAddress2()) * oneMM, Util.getFieldPostionY(fieldPosition.getReceiverAddress2()) * oneMM);
		}
		
		g2.setFont(font);
		
		String buyer_phone = raddress.getReceiver_phone();
		String buyer_mobile = raddress.getReceiver_mobile();
		
		String phone;
		
		if(buyer_phone != null && buyer_mobile != null) {
			phone = buyer_phone + " " + buyer_mobile;
		}  else if(buyer_phone != null) {
			phone = buyer_phone;
		} else {
			phone = buyer_mobile;
		}
		
		g2.drawString(phone, Util.getFieldPostionX(fieldPosition.getBuyerPhone()) * oneMM, Util.getFieldPostionY(fieldPosition.getBuyerPhone()) * oneMM);
		
		return Printable.PAGE_EXISTS; // 存在打印页时，继续打印工作
	}
	
	/**
	 * <p>过滤为Null的地址信息</p>
	 * @param address
	 * @return
	 */
	private String filterNullAddress(String address) {
		if(address == null)
			return "";
		return address;
	}
	
	/**
	 * <p>获取日期<p>
	 * @return
	 * 		[year, month, day]
	 */
	private String[] getData() {
		Calendar cal = Calendar.getInstance();
		
		return new String[]{String.valueOf(cal.get(Calendar.YEAR)), String.valueOf(cal.get(Calendar.MONTH) + 1), String.valueOf(cal.get(Calendar.DAY_OF_MONTH))};
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
	}

}

